using GameFrameWork;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

[CustomEditor(typeof(UIRectMesh), true)]
class UIRectMeshInspector : UIBaseMeshInspector
{
    SerializedProperty _rect;
    GameObject _go;
    Color _color = Color.green;
    BoxBoundsHandle _BoundsHandle = new();


    protected override void OnEnable()
    {
        base.OnEnable();

        _rect = serializedObject.FindProperty("rect");
    }

    protected override void OnDisable()
    {
        base.OnDisable();
    }

    protected override void OnDraw()
    {
        base.OnDraw();

        if (target is MonoBehaviour mono)
            _go = mono.gameObject;

        EditorGUILayout.BeginVertical("HelpBox");
        {
            EditorGUI.indentLevel++;

            _rect.rectValue = EditorGUILayout.RectField("Rect", _rect.rectValue);

            EditorGUI.indentLevel--;
            EditorGUILayout.EndVertical();
        }

        EditorGUILayout.Space(5);
    }

    void OnSceneGUI()
    {
        if (_go)
        {
            using (new Handles.DrawingScope(_color, _go.transform.localToWorldMatrix))
            {
                _BoundsHandle.center = _rect.rectValue.position;
                _BoundsHandle.size = _rect.rectValue.size;
                _BoundsHandle.axes = PrimitiveBoundsHandle.Axes.All;
                EditorGUI.BeginChangeCheck();
                _BoundsHandle.DrawHandle();
                if (EditorGUI.EndChangeCheck())
                {
                    _rect.rectValue = new Rect(_BoundsHandle.center, _BoundsHandle.size);
                    serializedObject.ApplyModifiedProperties();
                }
            }
        }
    }
}
